/**
 *******************************************************************************
 * @FileName  : tlsr825x_timer.c
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include "tlsr825x_timer.h"

/**
 * @brief : Timer initialization
 * @param : TIMx      can be TIM0/TIM1/TIM2 ...
 * @param : TIM_Mode  can be TIM_Mode_Sys_Clk/TIM_Mode_Gpio_Trigg ...
 * @return: none.
 */
void TIM_Init(uint8_t TIMx, uint8_t TIM_Mode)
{
	uint32_t *pCntReg = (uint32_t *)&TIM->TIM0_CNT + TIMx;
	*pCntReg = 0;

	uint32_t value = TIM->CTRL_STAT;

	switch(TIMx)
	{
	case TIM0:
		value &= ~(TIM_CTRL_TIM0_MOD|0xFF000000);
		TIM->CTRL_STAT  = (value | (TIM_Mode<<1));
		break;

	case TIM1:
		value &= ~(TIM_CTRL_TIM1_MOD|0xFF000000);
		TIM->CTRL_STAT = (value | TIM_Mode<<4);
		break;

	case TIM2:
		value &= ~(TIM_CTRL_TIM2_MOD|0xFF000000);
		TIM->CTRL_STAT = (value | (TIM_Mode<<7));
		break;

	default:
		break;
	}
}

/**
 * @brief : Timer enable/disable.
 * @param : TIMx      can be TIM0/TIM1/TIM2 ...
 * @param : enable    can be true/false.
 * @return: none.
 */
void TIM_Cmd(uint8_t TIMx, bool_t enable)
{
	uint32_t value = TIM->CTRL_STAT;
	value &= ~0xFF000000;

	switch(TIMx)
	{
	case TIM0:
		if(enable){
			TIM->CTRL_STAT = value | TIM_CTRL_TIM0_EN;
		}
		else{
			TIM->CTRL_STAT = (value | (~TIM_CTRL_TIM0_EN));
		}
		break;
	case TIM1:
		if(enable){
			TIM->CTRL_STAT = value | TIM_CTRL_TIM1_EN;
		}
		else{
			TIM->CTRL_STAT = (value | (~TIM_CTRL_TIM1_EN));
		}
		break;
	case TIM2:
		if(enable){
			TIM->CTRL_STAT = value | TIM_CTRL_TIM2_EN;
		}
		else{
			TIM->CTRL_STAT = (value | (~TIM_CTRL_TIM2_EN));
		}
		break;
	default:
		break;
	}
}

/**
 * @brief : Set Timer counter.
 * @param : TIMx      can be TIM0/TIM1/TIM2 ...
 * @param : counter
 * @return: none.
 */
void TIM_SetCounter(uint8_t TIMx, uint32_t counter)
{
	uint32_t *pCntReg = (uint32_t *)&TIM->TIM0_CNT + TIMx;
	*pCntReg = counter;
}

/**
 * @brief : Set Timer counter.
 * @param : TIMx      can be TIM0/TIM1/TIM2 ...
 * @param : cap
 * @return: none.
 */
void TIM_SetCapture(uint8_t TIMx, uint32_t cap)
{
	uint32_t *pCapReg = (uint32_t *)&TIM->TIM0_CAPT + TIMx;
	*pCapReg = cap;
}

/**
 * @brief : Timer get interrupt flag.
 * @param : TIMx      can be TIM0/TIM1/TIM2 ...
 * @return: none.
 */
RAM_CODE
uint8_t TIM_GetITFlag(uint8_t TIMx)
{
	uint8_t *pState = (uint8_t *)&TIM->CTRL_STAT + 3;
	return *pState & (1<<TIMx);
}

/**
 * @brief : Timer clear interrupt flag.
 * @param : TIMx      can be TIM0/TIM1/TIM2 ...
 * @return: none.
 */
RAM_CODE
void TIM_ClearITFlag(uint8_t TIMx)
{
	 uint8_t *pState = (uint8_t *)&TIM->CTRL_STAT + 3;
	 *pState |= (1<<TIMx);
}





